package com.fengjue.service.impl;

import com.fengjue.common.utils.JedisUtil;
import com.fengjue.common.utils.ObjectTranscoder;
import com.fengjue.pojo.Admin;
import com.fengjue.pojo.Menu;
import com.fengjue.mapper.MenuMapper;
import com.fengjue.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fengjue
 * @since 2021-03-10
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {


    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<Menu> getMenusByAdminId() {

        Admin principal = (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer adminId = principal.getId();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // 从redis取菜单数据
        List<Menu> menus = (List<Menu>) valueOperations.get("menu_" + adminId);

        if (CollectionUtils.isEmpty(menus)) {
            // 从数据库取菜单数据
            menus = menuMapper.getMenusByAdminId(adminId);
            // 取完存到redis里
            valueOperations.set("menu_" + adminId,menus);
        }else {
            System.out.println("走redis====================================");

        }        return menus;
    }

    @Override
    public List<Menu> getMenusWithRole() {
        Jedis resource = JedisUtil.getInstance().getPool().getResource();
        GenericJackson2JsonRedisSerializer gz = new GenericJackson2JsonRedisSerializer();
        final String s = "menusWithRole";
        byte[] menusWithRoleS = gz.serialize(s);
        byte[] bytes = resource.get(menusWithRoleS);
        if(bytes==null){
            List<Menu> allMenus = menuMapper.getMenusWithRole();
            byte[] serialize = gz.serialize(allMenus);
            resource.set(menusWithRoleS,serialize);
            return allMenus;
        }
        List<Menu> deserialize = (List<Menu>) gz.deserialize(bytes);
        resource.close();
        return deserialize;
    }

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public List<Menu> getAllMenus() {
        Jedis resource = JedisUtil.getInstance().getPool().getResource();
        final String s = "allMenus";
        byte[] allMenus1 = ObjectTranscoder.serialize(s);
        byte[] bytes = resource.get(allMenus1);
        if(bytes==null){
            List<Menu> allMenus = menuMapper.getAllMenus();
            byte[] serialize = ObjectTranscoder.serialize(allMenus);
            resource.set(allMenus1,serialize);
            return allMenus;
        }

        List<Menu> deserialize = (List<Menu>) ObjectTranscoder.deserialize(bytes);
        resource.close();
        return deserialize;
    }
}
